import {Rehydrater} from "./Rehydraters"
export interface cachedata{
    data:RoomObject[],
    lastupdate:number
}
export interface cachedataId{
    data:Id<RoomObject & _HasId>[],
    lastupdate:number
}
const tmp_heapcache = new Map<string,Record<string,cachedata>>();
const tmp_heapcacheid = new Map<string,Record<string,cachedataId>>();
export function CacheLookfor(interval:number)
{

    return function(tarage:RoomPosition, methodName: string, desc:any){


        var methodFun = desc.value;
        desc.value = function(...args:any)
        { // 重写 getData 方法

            const key = `${tarage.roomName}_${tarage.x}_${tarage.y}`;
            const argtype=args[0];
            let cachemap =  tmp_heapcache.get(key);
            if(cachemap)
            {
                if(cachemap[argtype] && cachemap[argtype].lastupdate>=Game.time)
                {
                    // 用缓存不用更新
                    return cachemap[argtype]
                }
            }
            else
            {
                cachemap={}
            }
            const ret =  methodFun.apply(this,args);
            cachemap[argtype]={data :ret,lastupdate:Game.time+interval};
            
            // 执行输出 我是getData方 

            return ret;
        }
     }
}
 

export function CacheIdLookfor(interval:number)
{

    return function(tarage:RoomPosition, methodName: string, desc:any){


        var methodFun = desc.value;
        desc.value = function(...args:any)
        { // 重写 getData 方法

            const key = `${tarage.roomName}_${tarage.x}_${tarage.y}`;
            const argtype=args[0];
            if(argtype == LOOK_CREEPS || argtype ==LOOK_POWER_CREEPS)
            {
                return  methodFun.apply(this,args);
            }
            let cachemap =  tmp_heapcacheid.get(key);
            if(cachemap)
            {
                if(cachemap[argtype] && cachemap[argtype].lastupdate>=Game.time)
                {
                    // 用缓存不用更新
                    return _.map(cachemap[argtype].data,Game.getObjectById)
                }
            }
            else
            {
                cachemap={}
            }
            // 检查ID是否过期
            let isexpires = true;
            if(cachemap[argtype]&&  cachemap[argtype].data)
            {
                isexpires = false;
                for(const id of cachemap[argtype].data)
                {
                    if(!Game.getObjectById(id))
                    {
                        isexpires = true;
                        break;
                    }
                }
            }
            if(isexpires)
            {
                const ret =  methodFun.apply(this,args);
                if(ret.length>0)
                {
                    cachemap[argtype]={data : _.map(ret,item=>(item as any).id),lastupdate:Game.time+interval};
                }
                return ret;
            }
            // 执行输出 我是getData方 

            return _.map(cachemap[argtype].data,Game.getObjectById)
        }
     }
}